package com.zrc.module_find.activity.defrecycleview

import android.content.Context
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ColorUtils
import com.zrc.superframe_base.ARouterHub
import com.zrc.superframe_base.mvx.mvvm.BaseMvvmActivity
import com.zrc.superframe_base.mvx.mvvm.BaseMvvmFragment
import com.zrc.superframe_ui.layout.SuperViewPagerAdapter
import com.zrc.superframe_base.widget.magicindicator.ColorFlipPagerTitleView
import com.zrc.module_find.R
import com.zrc.module_find.BR
import com.zrc.module_find.databinding.ActivityDefreclecviewBinding
import com.zrc.module_find.viewmodel.FindVM
import kotlinx.android.synthetic.main.activity_defreclecview.*
import kotlinx.android.synthetic.main.toolbar.*
import net.lucode.hackware.magicindicator.ViewPagerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
@Route(path = ARouterHub.ACTIVITY_DEFRECYCLEVIEW, name = "不一样的Recycleview")
class DefRecycleviewActivity : BaseMvvmActivity<ActivityDefreclecviewBinding, FindVM>() {

    lateinit var adapter: SuperViewPagerAdapter

    override fun getVariableId() = BR.viewModel

    override fun getLayoutId() = R.layout.activity_defreclecview

    override fun getViewModel() = FindVM(this)

    companion object {
        lateinit var mViewPager: ViewPager
    }

    override fun initView() {
        mViewPager= view_pager
        super.initView()
        toolbar_left.setOnClickListener { finish() }
        toolbar_center.text = "不一样的Recycleview"
        adapter = SuperViewPagerAdapter(supportFragmentManager)
        adapter.addFragment(ARouter.getInstance().build(ARouterHub.FRAGMENT_DEMO1).navigation() as BaseMvvmFragment<*, *>, "DEMO1", R.drawable.vector_error)
        adapter.addFragment(ARouter.getInstance().build(ARouterHub.FRAGMENT_DEMO2).navigation() as BaseMvvmFragment<*, *>, "DEMO2", R.drawable.vector_error)
        adapter.addFragment(ARouter.getInstance().build(ARouterHub.FRAGMENT_DEMO3).navigation() as BaseMvvmFragment<*, *>, "DEMO3", R.drawable.vector_empty)
        view_pager.adapter=adapter
        initMagicIndicator1()
    }

    private fun initMagicIndicator1() {
        val commonNavigator = CommonNavigator(this)
        //commonNavigator.scrollPivotX = 0.65f
        commonNavigator.isAdjustMode = true
        commonNavigator.adapter = object : CommonNavigatorAdapter() {

            override fun getCount(): Int=adapter.count

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView: SimplePagerTitleView = ColorFlipPagerTitleView(context)
                simplePagerTitleView.text = adapter.getPageTitle(index)
                simplePagerTitleView.normalColor = ColorUtils.getColor(R.color.gray_707070)
                simplePagerTitleView.selectedColor =  ColorUtils.getColor(R.color.green_1afa29)
                simplePagerTitleView.setOnClickListener {
                    view_pager.currentItem = index
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator {
                val indicator = LinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.lineHeight = UIUtil.dip2px(context, 6.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 10.0).toFloat()
                indicator.roundRadius = UIUtil.dip2px(context, 3.0).toFloat()
                indicator.startInterpolator = AccelerateInterpolator()
                indicator.endInterpolator = DecelerateInterpolator(2.0f)
                indicator.setColors(ColorUtils.getColor(R.color.green_1afa29))
                return indicator
            }
        }
        magicindicator.navigator = commonNavigator
        ViewPagerHelper.bind(magicindicator, view_pager)
    }

}